package com.zues.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RequestMappingController {


    @RequestMapping(
            value = {"testRequestMapping"}
    )
    public String testRequestMapping() {
        return "success";
    }


    @GetMapping("go_form")
    public String goFrom() {
        return "form";
    }

    /**
     * 测试post提交接受参数
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "test_post"
            , method = {RequestMethod.POST}
            , params = {"username", "password"})
    public String testFrom(String username, String password) {
//        System.out.println("username = " + username);
//        System.out.println("password = " + password);
        return "success";
    }

    /**
     * 测试restful风格接受参数
     * @param id
     * @param username
     * @return
     */
    @GetMapping("/testRest/{id}/{username}")
    public String testRest(@PathVariable("id") Integer id, @PathVariable("username") String username) {
        System.out.println("id:" + id + ",username:" + username);
        return "success";
    }

    /**
     * 通过ServletAPI获取
     * @param request
     * @return
     */
    @RequestMapping("/testParam")
    public String testParam(HttpServletRequest request){
        String username = request.getParameter("username");
        String id = request.getParameter("id");
        System.out.println("id:" + id + ",username:" + username);
        return "success";
    }
}
